#define ABS(x) (x-_start+0x7c00)
#define BOOTSP	    0x2000

.code16
.text

.global _start
_start:		# Label for the start of the main program
    # Normalize the start address $BOOTSEG in %cs
    cli	    # disable interrupts
    ljmp    $0, $ABS(main)
 
.byte 0
.byte 0
dap:
.byte 0x10  #size of DAP (Disk Address Packet)
.byte 0	    # magic number
.byte 1	    #number of sectors to read
.byte 0	    # magic number
.long 0x00008000    #segment:offest to the memory, hardcoded
.long 0x00000001    #LSB of first sector to read (indexed from 0)
.long 0x00000000    #MSB of sector to read (indexed from 0)

msg_buf:
.ascii "bootka\r\n"
.rept 8
    .byte 0
.endr

main:

	xorw    %ax, %ax
	# Location of data is DS:Offset
	#movw    %ax, %cs
	movw    %ax, %ds	# This can not be loaded directly it has to be in two steps.
	movw    %ax, %ss
	movw    $BOOTSP, %sp
	sti	    # enable interrupts
	cld	    # lodsb will increment %si

	movw    $ABS(msg_buf), %si # Load the string into position for the procedure.

	mov	    $0x0E, %ah	# The function to display a chacter (teletype)
	movw    $0x07, %bx	# Normal text attribute

nextchar:	# Internal label (needed to loop round for the next character)
	lodsb	# I think of this as LOaD String Block 
	# (Not sure if thats the real meaning though)
	# Loads [SI] into AL and increases SI by one

	# Check for end of string '0' 
	or	    %al, %al # Sets the zero flag if al = 0 
			# (OR outputs 0s where there is a zero bit in the register)
	jz	    die	    # If the zero flag has been set go to the end of the procedure.
			# Zero flag gets set when an instruction returns 0 as the answer.
	int	    $0x10   # Run the BIOS video interrupt 
	jmp	    nextchar    # Loop back round tothe top

die:	    # Label at the end to jump to when complete
	xorw    %ax, %ax

	#check if LBA is supported
	movb    $0x41, %ah
	movw    $0x55aa, %bx
	mov	    $0x80, %dl
	int	    $0x13
	jc	    die2

	mov	    $0x42, %ah
	mov	    $0x80, %dl
	movw    $ABS(dap), %si
	int	    $0x13
	jc	    die2

	ljmp    $0x800, $0x0	#jump to second stage, addr hardcoded

die2:
    int	    $0x18		#next boot device
#	int    $0x19		#bootloader reset
#	ljmp    $0xf000, $0xfff0	#BIOS reset code

